#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cairo

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib

from utils import tp_logging
from utils import tp_exceptions

logger = tp_logging.get_logger('line_dialog')


class LineDialog(Gtk.Dialog):

    def __init__(self, virtual_child=None, parent=None):
        Gtk.Dialog.__init__(self, 'dialog', parent, 0)

        self.virtual_child = virtual_child

        self.set_skip_taskbar_hint(True)

        self.set_can_default(False)

        self.set_can_focus(False)

        self.connect('close', Gtk.main_quit)

        self.connect('draw', self.on_draw)

        self.set_css()

        self.tran_setup()

        self.set_widgets()

        self.visible = False


    def set_css(self):
        css = Gtk.CssProvider()

        css.load_from_path('./static/style.css')

        self.set_decorated(False)

        screen = Gdk.Screen.get_default()

        visual = self.get_screen().get_rgba_visual()

        styleContext = Gtk.StyleContext()

        styleContext.add_provider_for_screen(
            screen,
            css,
            Gtk.STYLE_PROVIDER_PRIORITY_USER
        )

    def tran_setup(self):

        self.set_app_paintable(True)

        screen = self.get_screen()

        visual = screen.get_rgba_visual()

        if visual != None and screen.is_composited():
            self.set_visual(visual)

    def on_draw(self, wid, cr):

        cr.set_source_rgba(0, 0, 0, 0)
        cr.set_operator(cairo.OPERATOR_SOURCE)
        cr.paint()

    def set_widgets(self):

        box = self.get_content_area()

        grid = Gtk.Grid()

        self.top_line = Gtk.Label()

        self.top_line.set_name('hidden')

        grid.add(self.top_line)

        box.add(grid)

    @staticmethod
    def get_monitor_dimensions():
        display = Gdk.Display.get_default()

        monitor = Gdk.Display.get_monitor(display, 1) # Must specify which monitor

        return Gdk.Monitor.get_geometry(monitor)

    def reposition_dialog(self):
        monitor_dimensions = self.get_monitor_dimensions()
        monitor_width = monitor_dimensions.width
        monitor_height= monitor_dimensions.height

        dialog_dimensions = self.get_preferred_size()[0]
        dialog_width = dialog_dimensions.width

        y_axis = monitor_height - 90

        if (
                self.virtual_child is not None and
                self.virtual_child.visible is True):

            self.move((monitor_width/2)-(dialog_width/2), y_axis - 60)

        else:
            self.move((monitor_width/2)-(dialog_width/2), y_axis)


    def set_label(self, content_line=''):
        self.top_line.set_label(content_line)

        len_content_line = len(content_line)

        self.visible = True

        if len_content_line == 0:
            self.top_line.set_name('hidden')
            self.visible = False

        else:
            self.top_line.set_name('visible')
            self.visible = True

        self.reposition_dialog()

    def toggle_visibility(self, visibility):
        self.visible = visibility

def start_dialog(**kwargs):
    for dialog in kwargs.get('dialogs'):
        dialog.show_all()
    Gtk.main()

def end_dialog():
    Gtk.main_quit()

def glib_add(func, *args):
    GLib.idle_add(func, *args)

